package com.etrans.web.gps.business.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.base.domain.TerminalKind;
import com.etrans.web.gps.business.base.service.TerminalKindService;
import com.etrans.web.gps.common.page.PageResultVo;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.common.page.ResultContans;
import com.etrans.web.gps.util.grid.FlexiGridUtil;
import com.etrans.web.gps.util.grid.ParamMap;
import com.etrans.web.gps.util.grid.ParamValueObject;


@Controller
@RequestMapping("/base/terminalkind/*")
public class TerminalKindController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger("TerminalKindController");

	@Autowired
	private TerminalKindService terminalkindService;
	
	
	/**
	 * 页面跳转入口
	 * @return
	 */
	@RequestMapping(value="loadTerminalkind.html")
	public ModelAndView loadTerminalkind(){
		ModelAndView view = new ModelAndView();
		view.setViewName("views/base/terminalkind/terminalkind");
		return view;
		
	}

	/**
	 * 查询所有终端类型树
	 *
	 * @return List rows
     */
	@RequestMapping(value = "loadAllTerminalKindTrees.html")
	@ResponseBody
	public List loadAllTerminalKindTrees() {
		List rows = null;
		try {
		    rows = terminalkindService.loadAllTerminalKindTrees();
		} catch (Exception e) {
			logger.error("", e);
		}
		return rows;
	}

	/**
	 * 新增终端类型页面
	 *
	 * @return ModelAndView mav
     */
	@RequestMapping(value = "initTerminalKind.html")
	public ModelAndView initTerminalKind(@RequestParam String dbKey) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("action", "insert");
		mav.addObject("choose", dbKey);
		mav.addObject("addTerminalKind", new TerminalKind());
		mav.setViewName("views/base/terminalkind/addTerminalKindForm");
		return mav;
	}

	/**
	 * 加载终端类型信息
	 *
	 * @param request
	 * @param model
	 * @return ModelAndView mnv
     */
	@RequestMapping(value = "listTerminalKinds.html")
	public ModelAndView listTerminalKinds(HttpServletRequest request, Model model) {
		ModelAndView mnv = new ModelAndView();
		mnv.addObject("terminalkind", new TerminalKind());
		mnv.setViewName("secure/base/terminalkind/queryTerminalKind");
		return mnv;
	}

	/**
	 * 更新终端类型
	 *
	 * @param terminalkindId Integer
	 * @return ModelAndView mav
     */
	@RequestMapping(value = "updateTerminalKind.html")
	public ModelAndView updateTerminalKind(@RequestParam  Integer terminalkindId) {
		ModelAndView mav = new ModelAndView();
		try {
			TerminalKind terminalkind = terminalkindService.queryTerminalKindById(terminalkindId);
			terminalkind.setId(terminalkindId);
			mav.addObject("action", "update");
			mav.addObject("terminalkindId", terminalkindId);
			mav.addObject("terminalkind", terminalkind);
			mav.setViewName("views/base/terminalkind/addTerminalKindForm");
		} catch (Exception e) {
			logger.error("查找相应的终端类型", e);
		}
		return mav;
	}

	/**
	 * 保存终端类型
	 *
	 * @param action
	 * @param terminalkind
	 * @return PageResultVo  pr
     */
	@RequestMapping(value = "saveTerminalKind.html")
	@ResponseBody
	public PageResultVo saveTerminalKind(@RequestParam  String action, TerminalKind terminalkind) {
		PageResultVo pr = new PageResultVo();
		try {
			if ("insert".equals(action)) {
				terminalkindService.saveTerminalKind(terminalkind);
			}
			if ("update".equals(action)) {
				terminalkindService.updateTerminalKind(terminalkind);
			}
			pr.setCode(ResultContans.RETURN_OK_CODE.getCode());
			pr.setMsg("保存成功");
		} catch (Exception e) {
			logger.error("", e);
			pr.setCode(ResultContans.RETURN_ERROR_CODE.getCode());
			pr.setMsg("保存失败!");
		}
		return pr;
	}

	// 查询所有终端类型
	@RequestMapping(value = "loadAllTerminalKinds.html")
	@ResponseBody
	public PageVo loadAllTerminalKinds(HttpServletResponse response, String paramsGrid,@RequestParam String dbKey) {
		PageVo page = new PageVo();	
		Map<String, Object> paramMap =  FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
		List rows = terminalkindService.queryAllTerminalKinds(paramMap);
		page.setRows(rows);
		page.setPage((int)paramMap.get("page"));
		page.setTotal(terminalkindService.terminalkindCount(paramMap));
		return page;
	}
	
	//删除终端类型信息
	@RequestMapping(value = "delTerminalKind.html")
	@ResponseBody
	public PageResultVo delTerminalKind(String TerminalKindIds, @RequestParam String dbKey) {
		PageResultVo pr = new PageResultVo();
		try {
			if (TerminalKindIds != null && !"".equals(TerminalKindIds)) {
				for (String perID : TerminalKindIds.split(",")) {
					terminalkindService.deleteTerminalKindByID(Integer.parseInt(perID));
					pr.setCode(ResultContans.RETURN_OK_CODE.getCode());
				}
			}
		} catch (Exception e) {
			logger.error("", e);
			pr.setCode(ResultContans.RETURN_ERROR_CODE.getCode());
			pr.setMsg("删除失败!");
		}
		return pr;
	}
		
		/**
		 * 导出终端类型信息
		 * @param response
		 * @param paramsGrid
		 * @param StartPage
		 * @param EndPage
		 */
	@RequestMapping(value = "exportTerminalKinds.html")
	public void exportTerminalKinds(@RequestParam  String paramsGrid,Integer StartPage,Integer EndPage) {
		ParamMap param = FlexiGridUtil.parseJSONParam(paramsGrid);
		ParamValueObject inputParam = this.parseJSONParamExport(param,StartPage,EndPage);
		
		String[] titleArray = new String[2];
		titleArray[0]="终端类型名称";
		titleArray[1]="终端类型编号";
		
		String[] columnArray = new String[2];
		columnArray[0]="Name";
		columnArray[1]="Kind";
		
		List rows = terminalkindService.queryAllTerminalKinds(inputParam.getMapParams());
		//exportExl(request,response,"终端类型信息统计", titleArray, columnArray, rows);
	}
	/**
	 * 导出报表
	 * @param response
	 * @param request
	 * @param paramsGrid 前端json格式的参数
	 * @param colModel 表头
	 * @param title 标题
	 * @param imgsrc 图片url,如果为null则不加载图片页
	 */
	@RequestMapping(value="exportterminalkind.html")
	@ResponseBody
	public void exportterminalkind(@RequestParam HttpServletResponse response,HttpServletRequest request, String paramsGrid, String colModel,
			String title,String imgsrc,String StartPage,String EndPage,String pagesize ){
		
		Map<String, Object> paramMap=getReqParameters();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		
		
		List<TerminalKind> result = terminalkindService.queryAllTerminalKinds(paramMap);
		
		List<HashMap<String, String>> rows = new ArrayList<HashMap<String, String>>();
	
		int start=Integer.valueOf(Integer.valueOf(StartPage)-1)*Integer.valueOf(pagesize);
		int end=Integer.valueOf(EndPage)*Integer.valueOf(pagesize);
		if(end>(result.size()-1)){
			end=result.size();
		}
		for(int i=start;i<end;i++){
			HashMap<String, String> map = new HashMap<String, String>();
			TerminalKind obj=(TerminalKind)result.get(i);
			map.put("name", obj.getName());
			map.put("kind", String.valueOf(obj.getKind()));
			
			rows.add(map);
		}
		
		int index=-1;
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,null);
	}
	
	
	@RequestMapping(value = "checkIfExistName.html")
	@ResponseBody
	public Map<String, Object> checkIfExistNumber(@RequestParam String name,String id) {
		Map<String, Object> resultVo = new HashMap<>();
		HashMap<String, Object> params = new HashMap<>();
		params.put("name", name);
		params.put("ID", id);
		int Count = terminalkindService.queryCountByName(params);
		
		if (Count == 0) {
			resultVo.put("code", ResultContans.RETURN_OK_CODE.getCode());
		}
		else {
			resultVo.put("code", ResultContans.RETURN_REPENT_CODE.getCode());
		}
		return resultVo;
	}
	
	@RequestMapping(value = "checkIfExistKind.html")
	@ResponseBody
	public Map<String, Object> checkIfExistKind(@RequestParam String kind,String id) {
		Map<String, Object> resultVo = new HashMap<>();
		HashMap<String, Object> params = new HashMap<>();
		params.put("kind", kind);
		params.put("ID", id);
		int Count = terminalkindService.queryCountByKind(params);
		
		if (Count == 0) {
			resultVo.put("code", ResultContans.RETURN_OK_CODE.getCode());
		}
		else {
			resultVo.put("code", ResultContans.RETURN_REPENT_CODE.getCode());
		}
		return resultVo;
	}
}
